<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="160px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="12">
          <el-form-item label="缺陷分类编码:" prop="classifyIssueCode">
            <el-select
              v-model="formModel.classifyIssueCode"
              clearable
              filterable
              @clear="clearClassifyIssue"
              @change="changeClassifyIssue"
            >
              <el-option
                v-for="item in classifyIssueOptions"
                :key="item.classifyIssueCode"
                :label="item.classifyIssueCode+'-'+item.classifyIssueName"
                :value="item.classifyIssueCode"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="缺陷编码:" prop="issueCode">
            <el-select v-model="formModel.issueCode" clearable filterable>
              <el-option
                v-for="item in issueOptions"
                :key="item.issueCode"
                :label="item.issueCode+'-'+item.issueName"
                :value="item.issueCode"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部位编码:" prop="carAreaCode">
            <el-select
              v-model="formModel.carAreaCode"
              clearable
              filterable
              @clear="clearCarAreaCode"
              @change="changeCarAreaCode"
            >
              <el-option
                v-for="item in carAreaOptions"
                :key="item.carAreaCode"
                :label="item.carAreaCode+'-'+item.carAreaName"
                :value="item.carAreaCode"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部件编码:" prop="carModulesCode">
            <el-select v-model="formModel.carModulesCode" clearable filterable>
              <el-option
                v-for="item in carModulesOptions"
                :key="item.carModulesCode"
                :label="item.carModulesCode+'-'+item.carModulesName"
                :value="item.carModulesCode"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>

export default {
  name: 'ModulesIssueMnt',
  props: ['pageType', 'editData'],
  data() {
    return {
      // 缺陷分类列表
      classifyIssueOptions: [],
      // 缺陷列表
      issueOptions: [],
      // 部位列表
      carAreaOptions: [],
      // 部件列表
      carModulesOptions: [],
      formModel: {
        modulesIssueId: '',
        classifyIssueCode: '',
        issueCode: '',
        carAreaCode: '',
        carModulesCode: ''
      },
      // 老数据
      oldFormModel: {
      },
      rules: {}
    }
  },
  computed: {},
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.$form = this.$refs.form
      const { pageType, editData, formModel } = this
      if (pageType !== 'add') {
        this.assignData(editData, formModel)
        this.oldFormModel = JSON.parse(JSON.stringify(this.formModel))
        const param = {}
        this.getClassifyIssueOptions(param)
        this.getCarAreaOptions(param)
        if (this.formModel.classifyIssueCode !== '') {
          param.classifyIssueCode = this.formModel.classifyIssueCode
        }
        this.getIssueOptions(param)
        if (this.formModel.carAreaCode !== '') {
          param.carAreaCode = this.formModel.carAreaCode
        }
        this.getCarModulesOptions(param)
      } else {
        const param = {}
        this.getClassifyIssueOptions(param)
        this.getCarAreaOptions(param)
      }
      this.initRules()
    },
    initRules() {
      const REQUIRED_MSG = this.CONST.REQUIRED_MSG
      this.rules = {
        classifyIssueCode: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        issueCode: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        carAreaCode: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        carModulesCode: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }]
      }
      this.$nextTick(_ => {
        this.$form.clearValidate()
      })
    },
    /** 点击后根据选择的缺陷分类编码查询对应的缺陷列表 */
    changeClassifyIssue(value) {
      if (value !== '') {
        const param = { classifyIssueCode: value }
        this.formModel.issueCode = ''
        this.getIssueOptions(param)
      }
    },
    /** 清除选择的缺陷分类编码则清空缺陷列表 */
    clearClassifyIssue() {
      this.formModel.issueCode = ''
      this.issueOptions = []
    },
    /** 点击后根据选择的部位编码查询对应的部件列表 */
    changeCarAreaCode(value) {
      if (value !== '') {
        const param = { carAreaCode: value }
        this.formModel.carModulesCode = ''
        this.getCarModulesOptions(param)
      }
    },
    /** 清除选择的部位编码则清空部件列表 */
    clearCarAreaCode() {
      this.formModel.carModulesCode = ''
      this.carModulesOptions = []
    },
    // 获取缺陷分类列表
    getClassifyIssueOptions(param) {
      this.get('/classifyIssue/listClassifyIssue', param).then(res => {
        this.classifyIssueOptions = res
      })
    },
    // 获取缺陷列表
    getIssueOptions(param) {
      this.get('/issue/listIssue', param).then(res => {
        this.issueOptions = res
      })
    },
    // 获取部位列表
    getCarAreaOptions(param) {
      this.get('/carArea/listCarArea', param).then(res => {
        this.carAreaOptions = res
      })
    },
    // 获取部件列表
    getCarModulesOptions(param) {
      this.get('/carModules/listCarModules', param).then(res => {
        this.carModulesOptions = res
      })
    },
    // 提交新增或修改
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.formModel.modulesIssueId !== '') {
            // 判断是否修改
            if (JSON.stringify(this.formModel) !== JSON.stringify(this.oldFormModel)) {
              const tip = '修改成功！'
              const url = '/modulesIssue/updateModulesIssue'
              this.post(url, this.formModel).then(res => {
                this.$info(tip, () => {
                  this.$emit('mntSuc', res)
                  this.closeDialog()
                })
              })
            } else {
              this.$emit('mntSuc')
              this.closeDialog()
            }
          } else {
            const url = '/modulesIssue/saveModulesIssue'
            this.post(url, this.formModel).then(res => {
              const tip = '新增成功！'
              this.$info(tip, () => {
                this.$emit('mntSuc', res)
                this.closeDialog()
              })
            })
          }
        }
      })
    },
    closeDialog() {
      this.$parent.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
